package com.shtel.paas.monitorcenter.config.management.collection.config.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shtel.paas.monitorcenter.config.management.collection.config.vo.MonitoringIndexConfigurationVO;
import com.shtel.paas.monitorcenter.config.management.collection.config.vo.MonitoringIndexVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.shtel.paas.monitorcenter.config.management.collection.config.entity.CollectionConfigEntity;
import com.shtel.paas.monitorcenter.config.management.collection.config.entity.UpdateConfigurationCollectionEntity;
import com.shtel.paas.monitorcenter.config.management.collection.config.mapper.CollectionConfigMapper;
import com.shtel.paas.monitorcenter.config.management.collection.config.service.CollectionConfigService;
import com.shtel.paas.monitorcenter.config.management.collection.config.vo.CollectionConfigVo;
import com.shtel.paas.monitorcenter.config.management.collection.config.vo.SelectOptionValueVo;
import com.shtel.paas.monitorcenter.util.Page;

/**
 * 采集配置
 * 
 * @author zhaochuan.zhong
 *
 */
@Service
public class CollectionConfigServiceImpl implements CollectionConfigService {

	@Autowired
	private CollectionConfigMapper collectionConfigMapper;

	/**
	 * 获取指标配置信息。
	 */
	@Override
	public Page<CollectionConfigVo> selectCollectionConfigInfo(CollectionConfigEntity parameter) {
		PageHelper.startPage(parameter.getPage(), parameter.getSize());
		List<CollectionConfigVo> result = collectionConfigMapper.selectConfiguationCollectionInfo(parameter);
		Page<CollectionConfigVo> pageContent = Page.build(result);
		CollectionConfigVo dataAcquisitionFrequency = collectionConfigMapper.selectDataAcquisitionFrequency();
		dataAcquisitionFrequency.getDataAcquisitionFrequency();
		for(CollectionConfigVo obj:result){
			obj.setDataAcquisitionFrequency(dataAcquisitionFrequency.getDataAcquisitionFrequency());
		}
		pageContent.setData(result);
		return pageContent;
	}

	/**
	 * 更新指标配置信息。
	 */
	@Override
	public Integer updateCollectionConfigInfo(UpdateConfigurationCollectionEntity parameter) {
		Integer resultCount0 = 0;
		Integer resultCount1 = 0;
		if((!"".equals(parameter.getTimes()) && parameter.getTimes() != null) ||(!"".equals(parameter.getMinutes()) && parameter.getTimes() != null)){
			resultCount0 = collectionConfigMapper.updateCollectionConfigInfo(parameter);
		}
		if((!"".equals(parameter.getDataAcquisitionFrequency()) && parameter.getDataAcquisitionFrequency() != null)){
			resultCount1 = collectionConfigMapper.updateCollectionConfigInfoForXxlJobDb(parameter.getDataAcquisitionFrequency());
		}
		return resultCount0 + resultCount1;
	}

	/**
	 * 获取主机刷新信息。
	 */
	@Override
	public Page<CollectionConfigVo> selectCollectionConfigTab2Info(CollectionConfigEntity parameter) {
		PageHelper.startPage(parameter.getPage(), parameter.getSize());
		List<CollectionConfigVo> result = collectionConfigMapper.selectCollectionConfigTab2Info(parameter);
		for(CollectionConfigVo item:result){
			if(item.getRefreshTime().indexOf("m") != -1){
				item.setRefreshTime(item.getRefreshTime().split("m")[0] + "分钟");
			}else if(item.getRefreshTime().indexOf("h") != -1){
				item.setRefreshTime(item.getRefreshTime().split("h")[0] + "小时");
			}else if(item.getRefreshTime().indexOf("s") != -1){
				item.setRefreshTime(item.getRefreshTime().split("s")[0] + "秒");
			}else if(item.getRefreshTime().indexOf("d") != -1){
				item.setRefreshTime(item.getRefreshTime().split("d")[0] + "天");
			}
		}
		Page<CollectionConfigVo> pageContent = Page.build(result);
		return pageContent;
	}


	/**
	 * 更新主机刷新配置信息。
	 */
	@Override
	public Integer updateCollectionConfigTab2Info(UpdateConfigurationCollectionEntity parameter) {
		Integer resultCount = 0;
		if(!"".equals(parameter.getRefreshTime()) &&  parameter.getRefreshTime() != null)	{
			if(parameter.getRefreshTime().indexOf("分钟") != -1){
				parameter.setRefreshTime(parameter.getRefreshTime().split("分钟")[0] + "m");
			}else if(parameter.getRefreshTime().indexOf("小时") != -1){
				parameter.setRefreshTime(parameter.getRefreshTime().split("小时")[0] + "h");
			}else if(parameter.getRefreshTime().indexOf("秒") != -1){
				parameter.setRefreshTime(parameter.getRefreshTime().split("秒")[0] + "s");
			}else if(parameter.getRefreshTime().indexOf("天") != -1){
				parameter.setRefreshTime(parameter.getRefreshTime().split("天")[0] + "d");
			}
			resultCount = collectionConfigMapper.updateCollectionConfigTab2Info(parameter.getSytemParameterId(),parameter.getRefreshTime());
		}
		return resultCount;
	}


	/**
	 * 下拉框数据获取。
	 */
	@Override
	public Map<String,Object> selectOptionValue() {
		Map<String,Object> result = new HashMap<>();		//返回结果集
		List<SelectOptionValueVo> viewRefreshOptionValue = collectionConfigMapper.selectViewRefreshOptionValue();//视图刷新下拉框至获取
		SelectOptionValueVo dataAcquisitionFrequency = collectionConfigMapper.selectDataAcquisitionOptionFrequency();
		for(SelectOptionValueVo item:viewRefreshOptionValue){
			if(item.getName().indexOf("m") != -1){
				item.setName(item.getName().split("m")[0] + "分钟");
			}else if(item.getName().indexOf("h") != -1){
				item.setName(item.getName().split("h")[0] + "小时");
			}else if(item.getName().indexOf("s") != -1){
				item.setName(item.getName().split("s")[0] + "秒");
			}else if(item.getName().indexOf("d") != -1){
				item.setName(item.getName().split("d")[0] + "天");
			}
		}

		result.put("viewRefreshOptionValue", viewRefreshOptionValue);
		result.put("dataAcquisitionFrequency", dataAcquisitionFrequency);
		return result;
	}

	/**
	 * 查询数据采集频率
	 * creat by:zhangyy
	 * @param
	 * @return String
	 */
	@Override
	public String queryDataAcquisitionFrequency() {
		CollectionConfigVo dataAcquisitionFrequency = collectionConfigMapper.selectDataAcquisitionFrequency();
		return dataAcquisitionFrequency.getDataAcquisitionFrequency();
	}

	/**
	 * 查询监控指标ztree
	 * creat by:zhangyy
	 * @param
	 * @return list
	 */
	@Override
	public List<MonitoringIndexConfigurationVO>  selectMonitoringIndex() {
		List<MonitoringIndexConfigurationVO> childrenList= collectionConfigMapper.selectMonitoringIndex();
		//tree数据改造
		for(MonitoringIndexConfigurationVO monitoringIndexConfigurationVO:childrenList){
			monitoringIndexConfigurationVO.setIsParent(true);
			List<MonitoringIndexVO> childendListTemp =asyncGetKPITreeData(monitoringIndexConfigurationVO.getId()+"");
			monitoringIndexConfigurationVO.setChildren(childendListTemp);
		}
		return childrenList;
	}

	/**
	 * 查询监控指标ztree子节点
	 * creat by:zhangyy
	 * @param
	 * @return list
	 */
	@Override
	public  List<MonitoringIndexVO>  asyncGetKPITreeData(String id) {
		List<MonitoringIndexVO> list= collectionConfigMapper.asyncGetKPITreeData(id);
		//tree数据改造
		for(MonitoringIndexVO monitoringIndexVO:list){
			monitoringIndexVO.setIsParent(false);
		}
		return list;
	}

	/**
	 * 	修改数据采集频率
	 * creat by:zhangyy
	 * @param
	 * @return Integer
	 */
	@Override
	public  Integer  updataDataAcquisitionFrequency(String dataAcquisitionFrequency) {
		Integer resultCount = 0;
		if((!"".equals(dataAcquisitionFrequency) && dataAcquisitionFrequency != null)){
			resultCount = collectionConfigMapper.updateCollectionConfigInfoForXxlJobDb(dataAcquisitionFrequency);
		}
		return resultCount;
	}

	/**
	 * 	批量修改times AND minutes
	 * creat by:zhangyy
	 * @param
	 * @return Integer
	 */
	@Override
	public Integer updataTimesANDMinutes(UpdateConfigurationCollectionEntity parameter) {
		Integer resultCount= 0;
		if((!"".equals(parameter.getTimes()) && parameter.getTimes() != null) ||(!"".equals(parameter.getMinutes()) && parameter.getTimes() != null)){
			resultCount = collectionConfigMapper.updateCollectionConfigInfo(parameter);
		}
		return resultCount;
	}
}
